﻿#include "CameraManager.h"
#include "global.h"
#include <QImage>
bool IsColor(MvGvspPixelType enType)
{
    switch(enType)
    {
    case PixelType_Gvsp_BGR8_Packed:
    case PixelType_Gvsp_YUV422_Packed:
    case PixelType_Gvsp_YUV422_YUYV_Packed:
    case PixelType_Gvsp_BayerGR8:
    case PixelType_Gvsp_BayerRG8:
    case PixelType_Gvsp_BayerGB8:
    case PixelType_Gvsp_BayerBG8:
    case PixelType_Gvsp_BayerGB10:
    case PixelType_Gvsp_BayerGB10_Packed:
    case PixelType_Gvsp_BayerBG10:
    case PixelType_Gvsp_BayerBG10_Packed:
    case PixelType_Gvsp_BayerRG10:
    case PixelType_Gvsp_BayerRG10_Packed:
    case PixelType_Gvsp_BayerGR10:
    case PixelType_Gvsp_BayerGR10_Packed:
    case PixelType_Gvsp_BayerGB12:
    case PixelType_Gvsp_BayerGB12_Packed:
    case PixelType_Gvsp_BayerBG12:
    case PixelType_Gvsp_BayerBG12_Packed:
    case PixelType_Gvsp_BayerRG12:
    case PixelType_Gvsp_BayerRG12_Packed:
    case PixelType_Gvsp_BayerGR12:
    case PixelType_Gvsp_BayerGR12_Packed:
        return true;
    default:
        return false;
    }
}

bool IsMono(MvGvspPixelType enType)
{
    switch(enType)
    {
    case PixelType_Gvsp_Mono10:
    case PixelType_Gvsp_Mono10_Packed:
    case PixelType_Gvsp_Mono12:
    case PixelType_Gvsp_Mono12_Packed:
        return true;
    default:
        return false;
    }
}

CameraManager::CameraManager(QObject *parent) : QObject(parent)
{

}

CameraManager::~CameraManager()
{
    if(mycamera){
        closeCamera();
        delete mycamera;
        mycamera = nullptr;
    }
}

bool CameraManager::openCamera()
{
    MV_CC_DEVICE_INFO_LIST  m_stDevList;
    int nRet = CMvCamera::EnumDevices(MV_GIGE_DEVICE, &m_stDevList);
    if (MV_OK != nRet)
    {
        mylog<<"获取相机列表失败！ret:"<<nRet;
        return false;
    }
    nRet = mycamera->Open(m_stDevList.pDeviceInfo[0]);
    if (MV_OK != nRet)
    {
        mylog<<"打开相机失败！ret"<<nRet;
        return false;
    }
//    mylog<<"打开相机成功！";

    return true;
}

void CameraManager::closeCamera()
{
    int nRet = mycamera->Close();
    if(nRet != MV_OK)
    {
        mylog<<"关闭相机失败！ret:"<<nRet;
        return;
    }
    isCamConnect = false;
}

void CameraManager::slot_init()
{
    mycamera = new CMvCamera();
    bool ismistake = false;

    if(!openCamera()) return;

    do
    {
        int nRet = mycamera->SetEnumValueByString("TriggerMode", "On");
        if (MV_OK != nRet)
        {
            mylog<<QString("设置相机触发模式失败！%1").arg(QString::number(nRet));
            ismistake = true;
            break;
        }
        nRet = mycamera->SetEnumValueByString("TriggerSource", "Software");
        if (MV_OK != nRet)
        {
            mylog<<QString("设置相机软件触发失败！%1").arg(QString::number(nRet));
            ismistake = true;
            break;
        }
        nRet = mycamera->SetEnumValue("ExposureAuto",MV_EXPOSURE_AUTO_MODE_ONCE);
        if (MV_OK != nRet)
        {
            mylog<<QString("设置相机连续曝光失败！%1").arg(QString::number(nRet));
            ismistake = true;
            break;
        }
//        mycamera->SetFloatValue("ExposureTime",222222.0);
//        if (MV_OK != nRet)
//        {
//            mylog<<QString("设置相机曝光时间失败！%1").arg(QString::number(nRet)));
//            ismistake = true;
//            break;
//        }
    }while(0);

    if(ismistake)
    {
        closeCamera();
    }
    mylog<<"camera init success!";
    isCamConnect = true;
}

bool CameraManager::slot_catchImage(QImage& outputImage)
{
    uint packsize = 0;
    int nRet = mycamera->GetOptimalPacketSize(&packsize);
    if (MV_OK != nRet) {
        mylog << "获取最佳传输包大小失败！ret:" << nRet;
        return false;
    }

    if (packsize > 0) {
        nRet = mycamera->SetIntValue("GevSCPSPacketSize", packsize);
        if (nRet != MV_OK) {
            mylog << "设置最佳传输包大小失败！ret:" << nRet;
            return false;
        }
    }

    nRet = mycamera->StartGrabbing();
    if (MV_OK != nRet) {
        mylog << "开始抓取图像失败！ret:" << nRet;
        return false;
    }

    MV_FRAME_OUT stOutFrame;
    bool grabSuccess = false;

    do {
        nRet = mycamera->CommandExecute("TriggerSoftware");
        if (MV_OK != nRet) {
            mylog << "软触发失败！ret:" << nRet;
            break;
        }

        QThread::msleep(500);

        nRet = mycamera->GetImageBuffer(&stOutFrame, 0);
        if (nRet == MV_OK) {
            mylog << QString("成功获取到一幅图像，Width[%1], Height[%2], FrameNum[%3]")
                         .arg(QString::number(stOutFrame.stFrameInfo.nWidth))
                         .arg(QString::number(stOutFrame.stFrameInfo.nHeight))
                         .arg(QString::number(stOutFrame.stFrameInfo.nFrameNum));

            outputImage = QImage(stOutFrame.pBufAddr, stOutFrame.stFrameInfo.nWidth, stOutFrame.stFrameInfo.nHeight, QImage::Format_Grayscale8);
            mylog << outputImage;
            grabSuccess = true;
        } else {
            mylog << "获取一幅图像失败！ret:" << nRet;
        }

        nRet = mycamera->FreeImageBuffer(&stOutFrame);
        if (nRet != MV_OK) {
            mylog << "释放图像数组失败！ret:" << nRet;
            break;
        }
    } while (0);

    // 停止抓取图像
    nRet = mycamera->StopGrabbing();
    if (MV_OK != nRet) {
        mylog << "停止抓取图像失败！ret:" << nRet;
        return false;
    }

    return grabSuccess;
}
